/**
 * Created by Administrator on 2017/10/9 0009.
 */
/**
 * Phone-check
 */
var ip;
var storageName;
function check_phone() {
    var re = /^1[3|4|5|8][0-9]\d{8}$/;
    if (!re.test($("#phone").val())) {
        $("#phoneError").html("请输入正确的手机号！");
        $("#phone").addClass('error');
        return false;
    } else {
        $("#phone").removeClass('error');
        $("#phoneError").html("");
        return true;
    }
}

/**
 * Null-check
 */
function check_null(Id) {
    var id = '#' + Id;
    if ($(id).val() === '') {
        $(id + 'Error').html("不能为空！");
        $(id).addClass('error');
        return false;
    } else {
        $(id + 'Error').html("");
        $(id).removeClass('error');
        return true;
    }
}

/**
 * Num-check
 */
function check_num(Id) {
    var id = '#' + Id;
    var re = /^[0-9]+$/;
    var value = $(id).val();
    console.log(re.test(value));
    if (!re.test(value)) {
        $(id + 'Error').html("请输入纯数字！");
        return false;
    } else {
        $(id + 'Error').html("");
        return true;
    }
}

function check_ip_format(id) {
    var re = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/;
    id = '#'+ id;
    if (!re.test($(id).val())) {
        $(id + 'Error').html("请输入正确IP地址！");
        $(id).addClass('error');
        return false;
    } else {
        $(id + 'Error').html("");
        $(id).removeClass('error');
        return true;
    }
}

function check_storage() {
    if (check_null('name')) {
        var param = {
            uuid:$("#uuid").val(),
            name: $("#name").val()
        };
        var success = function (ret) {
            if (ret === true) {
                $("#nameError").html("仓库名已存在！");
                console.log('false');
                // $("#name").addClass('error');
                storageName = false;
            } else {
                $("#nameError").html("");
                // $("#name").removeClass('error');
                storageName =  true;
            }
        };
        ajax('get', "checkStorageName", param, success)
    }
}

/**
 *Ip-check
 */
function check_ip() {
    var IP = $("#ip_address").val();
    if (check_ip_format('ip_address')) {
        var param = {ip_address:IP};
        var success = function (ret) {
            if (ret === true) {
                $("#ipError").html("仓库IP已存在！");
                $("#ip_address").addClass('error');
                ip = false;
            } else {
                $("#ipError").html("");
                $("#ip_address").removeClass('error');
                ip = true;
            }
        };
        ajax('post', 'checkIP', param, success)
    }
}

// $("#address").blur(function () {
//     check_null('address');
// });

$("#phone").change(function () {
    check_phone();
});

$("#ip_address").change(function () {
    check_ip();
});

$("#name").change(function () {
    check_storage();
});

var clickTag = 0;

function checkStorage() {
    console.log('submit');
    var ret = false;
    if (clickTag === 0) {
        clickTag = 1;
        check_storage();
        check_ip();
        check_phone();
        ret = (Boolean)(storageName && ip && check_phone());
        setTimeout(function () {
            clickTag = 0;
        }, 1000);
    }
    return ret;
}


//检测层数纯数字
$('#isDouble').change(function () {
   var floorCount = $('#floorCount').val();
   if (floorCount !== '' && floorCount != null){
       isDouble();
   }
});

//检测占位数为纯数字
$('#floorCount').change(function () {
    if (check_null('floorCount')) {
        check_num('floorCount');
    }
});

$('#selectType').change(selectType());

function selectType() {
    if ($("#selectType").val() == "") {
        $("#typeError").html("请选择类型");
        $("#shelveType").removeClass('error');
        return false;
    } else {
        $("#typeError").html("");
        $("#shelveType").removeClass('error');
        return true;
    }
}

function isDouble() {
    var isDouble = $("#isDouble").val();
    var floorCount = $("#floorCount").val();
    console.log('check');
    console.log(isDouble);
    console.log(floorCount);
    if( isDouble == 0 || (isDouble && floorCount%2 ===0)){
        $("#floorCountError").html("");
        return true;
    }else{
        $("#floorCountError").html("双面单层站位数需要双数");
        return false;
    }
}



function checkSpan() {
    var shelfCodeError = $("#shelvesCodeError").text();
    return shelfCodeError === '';
}

$("#shelvesCode").change(function () {
    if (check_null('shelvesCode')) {
        check_num('shelvesCode');
    }
});

var clickShelf = 0;

function checkShelf() {
    var ret = false;
    if (clickShelf === 0) {
        clickShelf = 1;
        ret = (check_num('floor') && check_num('shelvesCode') && check_num('floorCount')  && selectType()  && checkSpan() && isDouble());
        setTimeout(function () {
            clickShelf = 0;
        }, 1000);
    }
    return ret;
}
